package com.unicorn.modual.user.service.impl;

import com.sun.xml.internal.ws.model.RuntimeModelerException;
import com.unicorn.entity.Users;
import com.unicorn.entity.enums.DataStaus;
import com.unicorn.entity.enums.UserStatus;
import com.unicorn.modual.user.dao.UsersDao;
import com.unicorn.modual.user.service.UserService;
import com.unicorn.vo.BaseVO;
import com.unicorn.vo.UsersVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.security.krb5.internal.crypto.RsaMd5DesCksumType;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by windy on 16-11-3.
 */
@Service
public class UserServiceImpl implements UserService {

    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UsersDao usersDao;

    /**
     * 查找有效数据
     * @return
     */
    @Override
    public List<Users> listUsers() {
        List<Users> list = new ArrayList<Users>();
        try {
            list = usersDao.findByIsDelete(DataStaus.EXIST);

        } catch (Exception e) {
            logger.error(this.getClass()+" "+BaseVO.MSG_LOAD_DATA_FAIL);
            logger.error(e.getMessage());
        }

        return list;
    }

    /**
     * 查找所有数据
     * @return
     */
    @Override
    public List<Users> listAllUsers() {
        List<Users> list = new ArrayList<Users>();
        list = usersDao.findAll();


        return list;
    }




    @Override
    @Transactional
    public Users saveUser(UsersVO vo) {

        try {
            Users users = new Users();
            users.setEmail(vo.getEmail());
            users.setNote(vo.getNote());
            users.setPassword(vo.getPassword());
            users.setUsername(vo.getUsername());
            users = usersDao.save(users);
            return users;

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }

    @Transactional
    @Override
    public boolean deleteUsersById(Long id) throws RuntimeModelerException{
        Users users= usersDao.getOne(id);
        if(users==null){
            logger.error(this.getClass()+" "+BaseVO.MSG_LOAD_DATA_FAIL);
            throw  new RuntimeException(BaseVO.MSG_LOAD_DATA_FAIL);

        }
        users.setIsDelete(DataStaus.DELETE);
        users.setDeleteTime(new Date(System.currentTimeMillis()));
        usersDao.save(users);
        return true;
    }

    @Transactional
    @Override
    public Users updateUser(UsersVO usersVO) throws RuntimeModelerException{
        Users users = usersDao.findByIdAndIsDelete(usersVO.getId(), DataStaus.EXIST);
        if(users==null){
            logger.error(this.getClass()+" "+BaseVO.MSG_LOAD_DATA_FAIL);
            throw  new RuntimeException(BaseVO.MSG_LOAD_DATA_FAIL);
        }
        users.setEmail(usersVO.getEmail());
        users.setNote(usersVO.getNote());
        users.setPassword(usersVO.getPassword());
        users.setUsername(usersVO.getUsername());
        users = usersDao.save(users);
        return users;
    }

}
